package com.hht.study.javaBase.generics;

import javax.management.openmbean.ArrayType;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 * @author hht
 * @date 2022/3/4
 */
public class GenericsTest {

    public static void main(String[] args) {
        List<Integer> list=new ArrayList<>();

        list.add(11);
        Class<? extends List> clazz = list.getClass();

        try {
            //通过反射添加，因为反射是在运行时添加，运行时java会对泛型进行类型擦除
            Method add = clazz.getDeclaredMethod("add", Object.class);
            add.invoke(list,"ad");
        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
        //输出了包含int和string
        System.out.println(list);
    }
}
